package com.xteam.youin_supplier.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.adpter.AddOilDetailListViewAdapter;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseInfo;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.BeddingListItem;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 基本信息
 */
@EFragment(R.layout.fragment_base_info)
public class BaseInfoFragment extends BaseFragment {


    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listview)
    RecyclerView listView;

    @ViewById(R.id.month_total_l)
    TextView monthTotal;
    @ViewById(R.id.month_total_income)
    TextView monthTotalIncome;


    AddOilDetailListViewAdapter addOilDetailListViewAdapter;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.base_info_title));

        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadData();


    }

    private void loadData() {
        requestParams = "supplier_id="+ GlobalData.userInfo.getData().getSupplier_id();
        requestUrl = RequestAddr.baseInfo + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    BaseInfo baseInfo;
    private void dealData(Object object) {
        try {
            baseInfo = gson.fromJson(object.toString(), BaseInfo.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

        addOilDetailListViewAdapter = new AddOilDetailListViewAdapter(getActivity().getApplicationContext(), baseInfo);
        listView.setAdapter(addOilDetailListViewAdapter);

        monthTotal.setText(baseInfo.getData().getCount());
        monthTotalIncome.setText(baseInfo.getData().getSum());
    }

    @Click({R.id.back_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStackImmediate();
                break;
        }
    }
}
